﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Portal.WebControls;
using System.Globalization;
using TST.SharePoint.Shared;

namespace TST.WebParts
{
    public class PageColumnFilter : ContextFilterWebPart, IWebEditable
    {
        [Browsable(false),
        Personalizable(PersonalizationScope.Shared)]
        public Guid PageFieldID
        {
            get;
            set;
        }

        [Browsable(false),
        Personalizable(PersonalizationScope.Shared)]
        public String PageFieldName
        {
            get;
            set;
        }

        protected override string DesignTimeHtml
        {
            get
            {
                if (DatasourceContentType.IsWSS)
                {
                    return "This filter web part can only be used in publishing sites of Microsoft Office SharePoint Server.";
                }
                string title;
                if ((PageFieldID == null || PageFieldID == Guid.Empty) && String.IsNullOrEmpty(PageFieldName))
                {
                    return "Please select a field.";
                }
                if (PageField != null)
                {
                    title = string.Format("This webpart sends the value of the page field '{0}' to connected webparts.", PageField.Title);
                }
                else if (PageFieldID != null && PageFieldID != Guid.Empty)
                {
                    title = string.Format("Field with the current ID could not be found in this list ({0}).", PageFieldID.ToString("B"));
                }
                else
                {
                    title = string.Format("Field with the current internal name could not be found in this list ({0}).", PageFieldName);
                }
                return title;
            }
        }

        private SPField PageField
        {
            get
            {
                if (SPContext.Current.List != null)
                {
                    if (PageFieldID != null && PageFieldID != Guid.Empty)
                    {
                        foreach (SPField field in SPContext.Current.List.Fields)
                            if (field.Id == PageFieldID)
                                return field;
                    }
                    else if (!String.IsNullOrEmpty(PageFieldName))
                    {
                        foreach (SPField field in SPContext.Current.List.Fields)
                            if (String.Compare(field.InternalName, PageFieldName, true)==0)
                                return field;
                    }
                }
                return null;
            }
        }

        protected override string[] GetRawFilterValues()
        {
            SPListItem listItem = SPContext.Current.ListItem;
            if (listItem != null && PageField != null)
            {
                Field wssField = FieldFactory.GetField(PageField);
                return new string[] {wssField.GetTypedValueFormatted(listItem[PageField.Id])};
            }
            return null;
        }

        public override bool FilterConfigured
        {
            get
            {
                if (!base.FilterConfigured)
                {
                    return false;
                }
                return ((PageFieldID != null && PageFieldID != Guid.Empty) || !String.IsNullOrEmpty(PageFieldName));
            }
        }

        [Browsable(false)]
        public override bool Hidden
        {
            get
            {
                if (base.WebPartManager == null)
                {
                    return base.Hidden;
                }
                if (this.Zone == null)
                {
                    return false;
                }
                return !base.WebPartManager.DisplayMode.AllowPageDesign;
            }
            set
            {
                base.Hidden = value;
            }
        }

        [Browsable(false)]
        public override bool Visible
        {
            get
            {
                if (base.WebPartManager == null)
                {
                    return base.Visible;
                }
                if (this.Zone != null)
                {
                    return true;
                }
                return base.WebPartManager.DisplayMode.AllowPageDesign;
            }
            set
            {
                base.Visible = value;
            }
        }

        #region IWebEditable Members

        EditorPartCollection IWebEditable.CreateEditorParts()
        {
            List<EditorPart> newEditors = new List<EditorPart>();

            EditorPartCollection editors = base.CreateEditorParts();
            foreach (EditorPart part in editors)
                newEditors.Add(part);

            newEditors.Add(new PageColumnFilterEditor(this.ID));

            return new EditorPartCollection(newEditors);
        }

        object IWebEditable.WebBrowsableObject
        {
            get { return this; }
        }

        #endregion
    }
}
